from flask import Blueprint,render_template,g
from models.stat.StatDailySite import StatDailySite
import datetime
route_index = Blueprint('index_page',__name__ )

@route_index.route("/")
def index():
    current_user = g.current_user

    data = {
        'finance': {
            'today': 0,
            'month': 0
        },
        'member': {
            'today_new': 0,
            'month_new': 0,
            'total': 0
        },
        'order': {
            'today': 0,
            'month': 0
        },
        'shared': {
            'today': 0,
            'month': 0
        },
    }

    last_date = StatDailySite.query.order_by(StatDailySite.id.desc()).first().date
    list = StatDailySite.query.filter(StatDailySite.date <= last_date).order_by(StatDailySite.id.desc()).limit(30).all()
    if list:
        for item in list:
            data['finance']['month'] += item.total_pay_money
            data['member']['month_new'] += item.total_new_member_count
            data['member']['total'] = item.total_member_count
            data['order']['month'] += item.total_order_count
            data['shared']['month'] += item.total_shared_count
            if item.date == last_date:
                data['finance']['today'] = item.total_pay_money
                data['member']['today_new'] = item.total_new_member_count
                data['order']['today'] = item.total_order_count
                data['shared']['today'] = item.total_shared_count

    return render_template("index/index.html",current_user=current_user,data=data)